﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;

namespace Uninstall
{
   class Program
   {
      static void Main(string[] args)
      {
         string sysroot = Environment.SystemDirectory;
         Environment.CurrentDirectory = sysroot;
         string unistallToolPath = Path.Combine(sysroot, "msiexec.exe");
         string productCode = GetProductCode();
         if (string.IsNullOrEmpty(productCode))
            return;
         string unistallParam = @"/x " + productCode;

         System.Diagnostics.Process.Start(
            unistallToolPath, unistallParam);
      }

      static string GetProductCode()
      {
         System.Reflection.Assembly thisAssembly = Assembly.GetExecutingAssembly();
         string assemblyFolder = Path.GetDirectoryName(thisAssembly.Location);
         string uninstallDataFile = Path.Combine(assemblyFolder, "Uninstall.dat");
         if(!File.Exists(uninstallDataFile))
         {
            Console.WriteLine(Resource1.Err_MissUnistallData);
            Console.WriteLine(uninstallDataFile);
            Console.Read();
            return null;
         }
         StreamReader reader = new StreamReader(uninstallDataFile);
         string productCode = reader.ReadLine().Trim();
         reader.Close();
         Guid guid = Guid.Empty;
         if (!Guid.TryParse(productCode, out guid))
         {
            Console.WriteLine(Resource1.Err_Incorrect_UninstallData);
            Console.Read();
            return null;
         }
         return productCode; 
      }
   }
}
